import serial
import paho.mqtt.client as mqtt
import binascii
import subprocess
import os
from threading import Timer
import time
from testslip import *
pttFlag=False 
timer_interval=0.0004
def delayrun():
    strTemp = ser.readall()
    if strTemp!='':
        print(strTemp)
        slipModel=slip()
        slipModel.stream = strTemp
        strdecode = slipModel.decode()
        if strdecode.find('\x90') == 4:
            resultcode = analysisfunc(strTemp)
            ser.write(str(resultcode))
        else
            client.publish("tty1r",binascii.b2a_hex(strTemp))

    strTemp1 = binascii.b2a_hex(ser1.readall())
    if strTemp1!='':
        print(strTemp1)
        client.publish("tty2r",strTemp1)
    strTemp2 = binascii.b2a_hex(ser2.readall())
    if strTemp2!='':
        print(strTemp2)
        client.publish("tty3r",strTemp2)
    Timer(timer_interval,delayrun).start()

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("tty1t")
    client.subscribe("tty2t")
    client.subscribe("tty3t")
    client.subscribe("ptt")
def on_message(client, userdata, msg):
    global pttFlag
    print(msg.topic+" "+binascii.b2a_hex(str(msg.payload)))
    if msg.topic == "tty1t":
        ser.write(str(msg.payload))
    elif msg.topic == "tty2t":
        ser1.write(str(msg.payload))
    elif msg.topic == "tty3t":
        ser2.write(str(msg.payload))
    elif msg.topic == "ptt":
        if pttFlag == False:
            os.system("amixer cset numid=7 1")
            os.system("amixer cset numid=7 0")
            pttFlag=True
        if str(msg.payload)=="push":
            os.system("echo 0 > /sys/class/gpio/gpio5/value")
        else:
            os.system("echo 1 > /sys/class/gpio/gpio5/value")
if __name__ == '__main__':
    p=subprocess.Popen(["linphonec", "-c", "/root/mylinphonerc","-a"],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    universal_newlines=True) #this is for text communication
    p.stdin.write("unregister")
    p.stdin.write("register sip:7002@127.0.0.1 127.0.0.1 456")
    #p.stdin.write("autoanswer enable\n")
    result_first_line=p.stdout.readline()
    client = mqtt.Client()
    #clientptt = mqtt.Client()
    client.on_connect = on_connect
    #clientptt.on_connect = ptt_connect
    client.on_message = on_message
    #clientptt.on_message = ptt_message
    try:
        ser = serial.Serial('/dev/ttyO3', 38400, timeout=1)
        ser.timeout=0.0001
        ser1 = serial.Serial('/dev/ttyO1', 38400, timeout=1)
        ser1.timeout=0.0001
        ser2 = serial.Serial('/dev/ttyUSB0',38400,timeout=1)
        ser2.timeout=0.0001
        client.connect("127.0.0.1", 1883, 60)
        os.system("echo 5 > /sys/class/gpio/export")
        os.system("echo out > /sys/class/gpio/gpio5/direction")
        os.system("echo 1 > /sys/class/gpio/gpio5/value")
        os.system("amixer cset numid=7 1")
        os.system("amixer cset numid=7 0")
        t=Timer(timer_interval,delayrun)
        t.start()
#       clientptt.connect("localhost",1883,60)
#       clientptt.loop_forever()
        client.loop_forever()
    except KeyboardInterrupt:
        client.disconnect()
        ser.close()
